package com.smz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smz.entity.TReservoir;
import com.smz.service.TReservoirService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api
@CrossOrigin
@RestController
public class TReservoirController {

    @Autowired
    private TReservoirService reservoirService;


    @ApiOperation("通过条件查找所有货区")
    @GetMapping("/findCargoAreaMan")
    public ResponseEntity<List<TReservoir>> findCargoAreaMan(@RequestParam("wid") Integer wid, @RequestParam("trId") Integer trId, @RequestParam("time") String time) {
        ResponseEntity one = reservoirService.findOne(wid, trId, time);
        return one;
    }

    @ApiOperation("查询分页所有的allReservoir")
    @GetMapping("/findAllTReservoir")
    public ResponseEntity<Page> findAll(@RequestParam(value = "current", defaultValue = "1") Integer current, @RequestParam Integer row) {

        Page page = reservoirService.page(current, row);

        return ResponseEntity.ok(page);
    }

    @ApiOperation("添加货区")
    @PostMapping("/addTReservoir")
    public ResponseEntity<String> add(@RequestBody TReservoir tReservoir) {
        reservoirService.saveOrUpdate(tReservoir);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("修改货区")
    @PutMapping("/updateTReservoir")
    public ResponseEntity<String> update(@RequestBody TReservoir tReservoir) {
        reservoirService.saveOrUpdate(tReservoir);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("删除货区")
    @DeleteMapping("/deleteTReservoir/{id}")
    public ResponseEntity<String> delete(@PathVariable("id") Integer id) {
        reservoirService.removeById(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("查找改仓库所有货区")
    @GetMapping("/reservoir/{id}")
    public ResponseEntity<List<TReservoir>> findAllTReservoir(@PathVariable("id") Integer id) {
        List<TReservoir> list = reservoirService.list(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getWareId, id));
        return ResponseEntity.ok(list);
    }


    @ApiOperation("通过单个id查找货区")
    @GetMapping("/findTReservoirId/{trId}")
    public ResponseEntity<TReservoir> findOne(@PathVariable("trId") Integer trId) {

        TReservoir one = reservoirService.getOne(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getId, trId));
        return ResponseEntity.ok(one);

    }


    @ApiOperation("通过仓库id查找货区")
    @GetMapping("/findTReservoirByWareId/{wareId}")
    public ResponseEntity<List<TReservoir>> findOneReservoir(@PathVariable("wareId") Integer trId) {
        List<TReservoir> list = reservoirService.list(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getWareId, trId));
        return ResponseEntity.ok(list);

    }
}
